﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

using ECCIT;
using ECCIT.Data;
using OgilvyOne.Sharan.Model;
using OgilvyOne.Sharan.IDAL;

namespace OgilvyOne.Sharan.DAL
{
    public class WeiboUserDAL : IDALWeiboUser
    {
        private const string C_TABLE_NAME = "tbl_KOLWeiboAccount";
        private const string C_COL_USER_ID = "col_UserID";
        private const string C_COL_LOGIN_NAME = "col_LoginName";
        private const string C_COL_PASSWORD = "col_Password";
        private const string C_COL_USER_WEIBO_ID = "col_UserWeiboID";
        private const string C_COL_AGE = "col_Age";
        private const string C_COL_JOB = "col_Job";
        private const string C_COL_HOBBY = "col_Hobby";

        private const string C_SQL_SELECT_ALL_USERS = "select " + C_COL_USER_ID + "," + C_COL_LOGIN_NAME + "," + C_COL_PASSWORD + "," + C_COL_USER_WEIBO_ID + "," + C_COL_AGE + "," + C_COL_JOB + "," + C_COL_HOBBY + " from " + C_TABLE_NAME;
        private const string C_SQL_INSERT_USER = "insert into " + C_TABLE_NAME + " (" +
            C_COL_LOGIN_NAME + "," + C_COL_PASSWORD + "," + C_COL_USER_WEIBO_ID + "," + C_COL_AGE + "," + C_COL_JOB + "," + C_COL_HOBBY + ") values (" +
            "@" + C_COL_LOGIN_NAME + ",@" + C_COL_PASSWORD + ",@" + C_COL_USER_WEIBO_ID + ",@" + C_COL_AGE + ",@" + C_COL_JOB + ",@" + C_COL_HOBBY + ")";

        #region IDALWeiboUser 成员

        public List<WeiboUserModel> GetAllWeiboUsers()
        {
            try
            {
                List<WeiboUserModel> users = new List<WeiboUserModel>();

                using (IDataReader reader = DBHelper.ExecuteReader(C_SQL_SELECT_ALL_USERS))
                {
                    while (reader.Read())
                    {
                        int? id = (int)reader[C_COL_USER_ID];
                        string loginName = (string)reader[C_COL_LOGIN_NAME];
                        string pwd = (string)reader[C_COL_PASSWORD];
                        long? weiboUserID = (long?)reader[C_COL_USER_WEIBO_ID];
                        int? age = (int?)reader[C_COL_AGE];
                        string job = (string)reader[C_COL_JOB];
                        string hobby = (string)reader[C_COL_HOBBY];

                        users.Add(new WeiboUserModel(id, loginName, pwd, weiboUserID, age, job, hobby));
                    }
                }

                return users;
            }
            catch (Exception ex)
            {
                throw new Exception("Error occurred in WeiboUserDAL.GetAllWeiboUsers(). Message: " + ex.Message);
            }
        }

        public ResultInfo Save(WeiboUserModel modelInfo)
        {
            if (modelInfo == null) throw new ArgumentNullException("modelInfo");

            ResultInfo result = new ResultInfo(false);

            try
            {
                int rowsAffected = 0;
                if (modelInfo.ID == null)   // insert
                {
                    //rowsAffected = Insert(modelInfo, null);
                    string cmdInsert = C_SQL_INSERT_USER + "; select @@identity";
                    IDataParameter[] parms = new SqlParameter[6];
                    parms[0] = new SqlParameter(C_COL_LOGIN_NAME, SqlDbType.NVarChar, 50);
                    parms[1] = new SqlParameter(C_COL_PASSWORD, SqlDbType.NVarChar, 50);
                    parms[2] = new SqlParameter(C_COL_USER_WEIBO_ID, SqlDbType.BigInt);
                    parms[3] = new SqlParameter(C_COL_AGE, SqlDbType.Int);
                    parms[4] = new SqlParameter(C_COL_JOB, SqlDbType.NVarChar, 50);
                    parms[5] = new SqlParameter(C_COL_HOBBY, SqlDbType.NVarChar, 200);
                    parms[0].Value = modelInfo.LoginName;
                    parms[1].Value = modelInfo.Password;
                    parms[2].Value = modelInfo.WeiboUserID;
                    parms[3].Value = modelInfo.Age;
                    parms[4].Value = modelInfo.Job;
                    parms[5].Value = modelInfo.Hobby;

                    object o = DBHelper.ExecuteScalar(cmdInsert, parms);
                    if (o != null && o != DBNull.Value)
                        modelInfo.ID = Convert.ToInt32(o);
                    //
                    rowsAffected = 1;
                }
                else // update
                {
                    //rowsAffected = UpdateByPrimaryKey(modelInfo, null);

                }
                //问题出在这里，判断条件错了,已更改为在插入操作时值变为1
                if (rowsAffected > 0)
                {
                    result.Message = "会员数据保存成功！";
                    result.Result = true;
                    result.ReturnObject = modelInfo;
                }
            }
            catch (Exception ex)
            {
                result.Exception = ex;
                result.Message = string.Format("保存会员数据时发生异常。异常信息：{0}", ex.Message);
            }

            return result;
        }

        public ResultInfo Delete(int userID)
        {
            ResultInfo result = new ResultInfo(false);
            try
            {
                string cmd = "delete from " + C_TABLE_NAME + " where " + C_COL_USER_ID + "=" + userID;
                DBHelper.ExecuteNonQuery(cmd);
                result.Result = true;
                result.Message = "会员数据删除成功！";
            }
            catch (Exception ex)
            {
                result.Message = string.Format("删除会员数据时发生异常。异常信息：{0}", ex.Message);
            }
            
            return result;
        }

        #endregion
    }
}